package cn.doctorpda.conference.ui.recommend;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.adapter.ConferenceListAdapter;
import cn.doctorpda.conference.adapter.RecommendReportAdapter;
import cn.doctorpda.conference.bean.DataInfo;
import cn.doctorpda.conference.bean.DataReportInfo;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.RecomHotInfo;
import cn.doctorpda.conference.bean.ReportInfoItem;
import cn.doctorpda.conference.bean.doc.DataDocInfo;
import cn.doctorpda.conference.presenter.DocPresenter;
import cn.doctorpda.conference.presenter.RecommendPresenter;
import cn.doctorpda.conference.presenter.ReportPresenter;
import cn.doctorpda.conference.ui.BaseListFragment;
import cn.doctorpda.conference.ui.common.DetailActivity;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.view.callbackview.IDocPageView;
import cn.doctorpda.conference.view.callbackview.IRecommendView;
import cn.doctorpda.conference.view.callbackview.IReportPageView;
import cn.doctorpda.conference.view.recyclerview.XRecylcerView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/23.
 */
public class ConferenceListFragment extends BaseListFragment implements SwipeRefreshLayout.OnRefreshListener, XRecylcerView.LoadingListener, IRecommendView, IDocPageView ,IReportPageView{

    private String type;
    private String address;
    private RecommendPresenter recommendPresenter;
    private DocPresenter docPresenter;
    private ConferenceListAdapter conferenceListAdapter;
    private RecommendReportAdapter recommendReportAdapter;
    private ReportPresenter reportPresenter;

    public static ConferenceListFragment newInstance(String type, String address) {
        ConferenceListFragment fragment = new ConferenceListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        bundle.putString("address", address);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getString("type");
        address = getArguments().getString("address");

        conferenceListAdapter = new ConferenceListAdapter(getActivity());

        conferenceListAdapter.setOnItemClickListener(new ConferenceListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, RecomHotInfo info) {
                L.d("链接推荐", info.getID() + info.getURL());

                DetailActivity.startActivity(getActivity(), info.getNAME(),info.getID(), info.getNAME(), info.getURL());
            }
        });

        recommendReportAdapter = new RecommendReportAdapter();

        recommendPresenter = new RecommendPresenter(this);
        docPresenter = new DocPresenter(this);

        reportPresenter = new ReportPresenter(this);

        if (type.equals("local")) {
            recommendPresenter.getLocalResult("1", "10", "");
        } else if (type.equals("physicians")) {
            recommendPresenter.getPhysicianResult("1", "10");
        } else if (type.equals("hot")) {
            recommendPresenter.getRecomHotData("1", "10");
        } else if (type.equals("report")) {
            // TODO 获取精彩报道接口
            reportPresenter.getReport("1", "10", "3","");
        } else if (type.equals("info")) {
            docPresenter.getDocData("1", "10", "");
        }

    }


    @Override
    public String getTitle() {
        if (type.equals("local")) {
            return "本地会议";
        } else if (type.equals("physicians")) {
            return "中国医师协会合作会议";
        } else if (type.equals("hot")) {
            return "全国热门";
        } else if (type.equals("report")) {
            return "精彩报道";
        } else if (type.equals("info")) {
            return "热门资料";
        }
        return "列表";
    }

    @Override
    public RecyclerView.Adapter getAdapter() {

        if (type.equals("report")) {

            return recommendReportAdapter;

        } else if (type.equals("info")) {
            return recommendReportAdapter;
        }
        return conferenceListAdapter;
    }

    @Override
    public Fragment getFragment() {
        return this;
    }

    @Override
    public String getPageName() {
        return "ConferenceListFragment";
    }

    @Override
    public void onLoadMore() {
        if (type.equals("local")) {
            recommendPresenter.getLocalResult(String.valueOf(conferenceListAdapter.getIndex()), "10", "");
        } else if (type.equals("physicians")) {
            recommendPresenter.getPhysicianResult(String.valueOf(conferenceListAdapter.getIndex()), "10");
        } else if (type.equals("hot")) {
            recommendPresenter.getRecomHotData(String.valueOf(conferenceListAdapter.getIndex()), "10");
        } else if (type.equals("report")) {
            // TODO 获取精彩报道接口
            reportPresenter.getReport(String.valueOf(recommendReportAdapter.getIndex()), "10", "3","");
        } else if (type.equals("info")) {
            docPresenter.getDocData(String.valueOf(conferenceListAdapter.getIndex()), "10", "");
        }
    }

    @Override
    public void onRefresh() {
      super.ll_no_data.setVisibility(View.INVISIBLE);
        if (type.equals("local")) {
            recommendPresenter.getLocalResult("1", "10", "");
        } else if (type.equals("physicians")) {
            recommendPresenter.getPhysicianResult("1", "10");
        } else if (type.equals("hot")) {
            recommendPresenter.getRecomHotData("1", "10");
        } else if (type.equals("report")) {
            // TODO 获取精彩报道接口
            reportPresenter.getReport("1", "10", "3","");
        } else if (type.equals("info")) {
            docPresenter.getDocData("1", "10", "");
        }
    }

    @Override
    public void setRPhysicianResult(Msg msg, DataInfo data) {
        recyclerView.setPreviousTotal(0);
        recyclerView.setIsnomore(false);
        swipeRefreshLayout.setRefreshing(false);
        conferenceListAdapter.clear();
        L.d("yishhi");
        if (data != null) {
            if (data.getPAGE().getList().isEmpty()) {
                super.ll_no_data.setVisibility(View.VISIBLE);
                super.imv_no_data.setBackgroundResource(R.drawable.nodata);
                super.tv_no_data.setVisibility(View.VISIBLE);
                super.tv_no_data.setText("");
            }else {

                super.ll_no_data.setVisibility(View.INVISIBLE);
            }
            conferenceListAdapter.addAll(data.getPAGE().getList());
        }else {
            super.ll_no_data.setVisibility(View.VISIBLE);
            super.tv_no_data.setVisibility(View.INVISIBLE);
            super.imv_no_data.setBackgroundResource(R.drawable.shibai);
        }
//        conferenceListAdapter.addAll(data.getPAGE().getList());
    }

    @Override
    public void setLPhysicianResult(Msg msg, DataInfo data) {
        recyclerView.loadMoreComplete();
        if(data!=null)
        conferenceListAdapter.addAll(data.getPAGE().getList());
    }

    @Override
    public void setPhysicianError(Call call, Exception e) {
        conferenceListAdapter.clear();
        recyclerView.loadMoreComplete();
        swipeRefreshLayout.setRefreshing(false);
        super.ll_no_data.setVisibility(View.VISIBLE);
        super.tv_no_data.setVisibility(View.INVISIBLE);
        super.imv_no_data.setBackgroundResource(R.drawable.shibai);
    }

    @Override
    public void setRLocalResult(Msg msg, DataInfo data) {
        recyclerView.setPreviousTotal(0);
        recyclerView.setIsnomore(false);
        swipeRefreshLayout.setRefreshing(false);
        conferenceListAdapter.clear();
        conferenceListAdapter.addAll(data.getPAGE().getList());
    }

    @Override
    public void setLLocalResult(Msg msg, DataInfo data) {
        recyclerView.loadMoreComplete();
        conferenceListAdapter.addAll(data.getPAGE().getList());
    }


    @Override
    public void setLocalError(Call call, Exception e) {

    }

    @Override
    public void setRHotResult(Msg msg, DataInfo data) {
        recyclerView.setPreviousTotal(0);
        recyclerView.setIsnomore(false);
        swipeRefreshLayout.setRefreshing(false);
        conferenceListAdapter.clear();
        if(data!=null)
        conferenceListAdapter.addAll(data.getPAGE().getList());
    }

    @Override
    public void setLHotResult(Msg msg, DataInfo data) {
        recyclerView.loadMoreComplete();
        conferenceListAdapter.clear();
        if(data!=null)
        conferenceListAdapter.addAll(data.getPAGE().getList());
    }


    @Override
    public void setHotError(Call call, Exception e) {

    }

    //报道
    @Override
    public void setRReportResult(Msg msg, DataReportInfo data) {
        recyclerView.setPreviousTotal(0);
        recyclerView.setIsnomore(false);
        swipeRefreshLayout.setRefreshing(false);
        recommendReportAdapter.clear();
        if(data!=null)
        recommendReportAdapter.addAll(data.getList());
    }

    @Override
    public void setLReportResult(Msg msg, DataReportInfo data) {
        recyclerView.loadMoreComplete();
        recommendReportAdapter.clear();
        if(data!=null)
        recommendReportAdapter.addAll(data.getList());
    }


    @Override
    public void setReportError(Call call, Exception e) {

    }

    //资料
    @Override
    public void setPageRefreshResult(Msg msg, DataDocInfo data) {

    }

    @Override
    public void setPageLoadResult(Msg msg, DataDocInfo data) {

    }

    @Override
    public void setPageError(Call call, Exception e) {

    }
}
